package com.ky.manage.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Vibrator;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import com.ky.manage.constant.OverallData;

import me.jessyan.autosize.utils.LogUtils;


/**
 * Created by dhc on 2017/6/9.
 */

public class SystemUtil {
    private static Handler hd = new Handler();
    private static String version = "";
    private static Vibrator vibrator;

    private static String getVersionName(Context context) {  //获取当前APP版本号
        // 获取packagemanager的实例
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (packInfo != null && !TextUtils.isEmpty(packInfo.versionName)) {
            version = packInfo.versionName;
        }
        return version;
    }

    public static String getAppVersion() {
        if (TextUtils.isEmpty(version)) {
            return getVersionName(OverallData.app);
        }
        return version;
    }

    /**
     * 获取当前app version code
     *
     * @param context
     */
    public static long getAppVersionCode(Context context) {
        long appVersionCode = 0;
        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = packageInfo.getLongVersionCode();
            } else {
                appVersionCode = packageInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
        }
        LogUtils.d("getAppVersionCode appVersionCode = " + appVersionCode);
        return appVersionCode;
    }

    /**
     * 获取当前app version code
     *
     * @param context
     * @param packageName
     */
    public static long getAppVersionCode(Context context, String packageName) {
        long appVersionCode = 0;
        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(packageName, 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = packageInfo.getLongVersionCode();
            } else {
                appVersionCode = packageInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
        }
        LogUtils.d("getAppVersionCode appVersionCode = " + appVersionCode);
        return appVersionCode;
    }

    /**
     * 获取app文件的 version code
     *
     * @param context
     * @param filePath
     */
    public static long getApkVersionCode(Context context, String filePath) {
        long appVersionCode = 0;
        try {
            PackageManager packageManager = context.getApplicationContext()
                    .getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = packageInfo.getLongVersionCode();
            } else {
                appVersionCode = packageInfo.versionCode;
            }
        } catch (Exception e) {
        }
        LogUtils.d("getAppVersionCode appVersionCode = " + appVersionCode);
        return appVersionCode;
    }

    public static void setStatusBar(Activity activity, boolean useThemestatusBarColor, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = activity.getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                activity.getWindow().setStatusBarColor(activity.getResources().getColor(color));
            } else {
                activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
}
